package com.costeaalex.datedrivenfilecopy.gui;

/**
 * @author Alex Costea
 * 
 * GUI creation. 
 * 
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

import com.costeaalex.datedrivenfilecopy.controller.Controller;

public class Gui implements Observer,ComponentListener
	{
	static int selectedItem=-1;
	
	private JFrame jF;
	private FTPJDialog jD;
	private JButton jBSetTime;
	private JButton jBAction;
	private JButton jBSource;
	private JButton jBDestination;
	private Container container;
	private JFileChooser fc1,fc2;
	private JPanel jPE;
	private JTextField jTF;
	private JCheckBox jCBErase;
	private JCheckBox jCBFTP;
	private JLabel jLInfo;
	private JLabel jLFile;
	private Path argGlobal;
	
	public Gui()
		{
		}
	
	public void buildGui()
		{		
		jF=new JFrame("Date driven file copy");
		jF.setSize(400,350);
		jF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jF.addComponentListener(this);
		
		jPE=new JPanel();
		jPE.setLayout(new BorderLayout());
		jPE.setBackground(Color.white);
		Border blackline = BorderFactory.createLineBorder(Color.blue, 3);
		jPE.setBorder(blackline);
		
		fc1=new JFileChooser(".");
		fc2=new JFileChooser(".");
		jBSetTime=new JButton("Set current time");
		jBAction=new JButton("Go");
		jBSource=new JButton("Select source");
		jBDestination=new JButton("Select destination");
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		Date dateobj = new Date();
		jTF=new JTextField();
		jTF.setText(df.format(dateobj));
		jCBErase=new JCheckBox("Check to erase previous files?");
		jCBFTP=new JCheckBox("Copy to FTP location.");
		jLInfo=new JLabel();
		jLFile=new JLabel();
		
		container=jF.getContentPane();
		container.setLayout(new BorderLayout());
		
		JPanel jp1=new JPanel();
		jp1.setLayout(new BoxLayout(jp1, BoxLayout.PAGE_AXIS));
		
		jp1.add(new JLabel("Select start date:"));
		jp1.add(jTF);
		jp1.add(Box.createRigidArea(new Dimension(0,5)));
		jp1.add(jBSetTime);
		jp1.add(Box.createRigidArea(new Dimension(0,5)));
		jp1.add(new JSeparator(SwingConstants.HORIZONTAL));
		jp1.add(Box.createRigidArea(new Dimension(0,5)));
		jp1.add(jCBErase);
		jp1.add(Box.createRigidArea(new Dimension(0,5)));
		jp1.add(new JSeparator(SwingConstants.HORIZONTAL));
		jp1.add(Box.createRigidArea(new Dimension(0,5)));
		jp1.add(jBSource);
		jp1.add(Box.createRigidArea(new Dimension(0,5)));
		jp1.add(jBDestination);
		jp1.add(jCBFTP);
		jp1.add(Box.createRigidArea(new Dimension(0,5)));
		jp1.add(new JSeparator(SwingConstants.HORIZONTAL));
		jp1.add(Box.createRigidArea(new Dimension(0,5)));
		jp1.add(jBAction);
		jp1.add(jLInfo);
		jp1.add(jLFile);
	
		container.add(jp1, BorderLayout.NORTH);
		
		jF.setVisible(true);
		
		//jDialog creation
		jD=new FTPJDialog(jF, "FTP Details");
		
		}
	
	//When the JFrame resizes make the components inside resize
	public void componentResized(ComponentEvent evt)
		{
	    SwingUtilities.invokeLater(new Runnable()
			{
			public void run()
				{
				}
			});
		}
	
	public void componentHidden(ComponentEvent e)
		{
		}

	public void componentMoved(ComponentEvent e)
		{
		}

	public void componentShown(ComponentEvent e)
		{
		}
	
	//Register the controller.
	public void registerController(Controller c) {
		jBAction.addActionListener(c);
		jD.getOKButton().addActionListener(c);
		jBSetTime.addActionListener(c);
		jBSource.addActionListener(c);
		jBDestination.addActionListener(c);
	}
	
	//SETTERS & GETTERS for the GUI.
	public void setStatus(String status) {
		setLabelText(jLInfo, status);
	}
	
	public boolean isSelectedErase() {
		return jCBErase.isSelected();
	}
	
	public boolean isSelectedFTP() {
		return jCBFTP.isSelected();
	}
	
	public String getSource() {
		return fc1.getSelectedFile().toPath().toString().replace("\\", "/");
	}
	
	public String getDestination() {
		if(jCBFTP.isSelected())
			return jD.getDestination()+"/";
		return fc2.getSelectedFile().toPath().toString().replace("\\", "/")+"/";
	}
	
	public String getDate() {
		return jTF.getText();
	}
	
	public String[] getConnectionData()	{
		return jD.getConnectionData();
	}
	
	public void hideFTPPopup() {
		jD.setVisible(false);
	}
	
	public void setCurrentTime() {
		SwingUtilities.invokeLater(new Runnable()
		{
		public void run()
			{
			DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
			Date dateobj = new Date();
			jTF.setText(df.format(dateobj));
			}
		});
	}
	
	public void setGoEnabled(boolean e) {
		jBAction.setEnabled(e);
	}
	
	public void showSourceSelectionPopup() {
		fc1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fc1.setAcceptAllFileFilterUsed(false);
		fc1.showDialog(jPE, "Select");
	}
	
	public void showDestinationSelectionPopup() {
		if(jCBFTP.isSelected()) {
			jD.setVisible(true);
		}
		else {
		fc2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fc2.setAcceptAllFileFilterUsed(false);
		fc2.showDialog(jPE, "Select");
		}
	}
	
	/*
	 * Gets updated with information from the copier/eraser;
	 * 
	 */
	public void update(Observable o, Object arg)
		{
		if(arg instanceof Path)//when copying/erasing
		{
		this.argGlobal=(Path) arg;
		if(jLInfo.getText()=="Done!")
			setLabelText(jLFile, "");
		else
			setLabelText(jLFile, "<html>"+(argGlobal).toString()+"</html>");
		}
		
		if(arg instanceof String && arg=="Done")//when copier is done
			{
			setLabelText(jLInfo, "Done!");
			setLabelText(jLFile, "");
			this.setGoEnabled(true);
			o.deleteObserver(this);
			}
		}
	
	//Sets status label.
	private void setLabelText(JLabel jTF, String str) {
		final String strLocal=str;
		final JLabel jTFLocal=jTF;
		SwingUtilities.invokeLater(new Runnable()
		{
		public void run()
			{
			jTFLocal.setText(strLocal);
			}
		});
	}
}